package com.skeleton.adminServer.guide.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.adminServer.guide.service.IAdminGuideService;
import com.skeleton.adminServer.guide.util.AdminGuideConstants;
import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.GuideCatgHotDto;
import com.skeleton.db.dto.GuideItemHotDto;
import com.skeleton.db.dto.PageDto;
import com.skeleton.utils.SystemTools;

@Controller
@RequestMapping("admin/guide/")
public class AdminGuideControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IAdminGuideService iAdminGuideService;

	@RequestMapping("adminAjaxAddHotCatg")
	@ResponseBody
	public Map<String, Object> adminAjaxAddHotCatg(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxAddHotCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String title = request.getParameter("title");
		String seq = request.getParameter("seq");
		GuideCatgHotDto gchDto = new GuideCatgHotDto();
		gchDto.setCatgHotId(0);
		gchDto.setCatgHotTitle(title);
		gchDto.setCatgHotSeq(Integer.parseInt(seq));

		ResultDto rd = iAdminGuideService.doAddHotCatg(gchDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxLoadHotCatg")
	@ResponseBody
	public Map<String, Object> adminAjaxLoadHotCatg(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxLoadHotCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		PageDto pDto = new PageDto(Integer.parseInt(page), Integer.parseInt(rows));

		ResultDto rd = iAdminGuideService.getAllHotCatgByPage(pDto);
		if (rd.isSuccess()) {
			List<GuideCatgHotDto> gchdList = rd.getDtoList("GuideCatgHotDtoList");
			map.put("rows", gchdList);
			pDto = (PageDto) rd.getFirstDto("PageDto");
			map.put("total", pDto.getTotal());
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxModifyHotCatg")
	@ResponseBody
	public Map<String, Object> adminAjaxModifyHotCatg(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxModifyHotCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String seq = request.getParameter("seq");

		GuideCatgHotDto gchDto = new GuideCatgHotDto();
		gchDto.setCatgHotId(Integer.parseInt(id));
		gchDto.setCatgHotTitle(title);
		gchDto.setCatgHotSeq(Integer.parseInt(seq));

		ResultDto rd = iAdminGuideService.doModifyHotCatg(gchDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxDeleteHotCatg")
	@ResponseBody
	public Map<String, Object> adminAjaxDeleteHotCatg(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxDeleteHotCatg-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");
		GuideCatgHotDto gchDto = new GuideCatgHotDto();
		gchDto.setCatgHotId(Integer.parseInt(id));

		ResultDto rd = iAdminGuideService.doDeleteHotCatg(gchDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxAddHotItem")
	@ResponseBody
	public Map<String, Object> adminAjaxAddHotItem(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxAddHotItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String catgId = request.getParameter("catgId");
		String title = request.getParameter("title");
		String url = request.getParameter("url");
		String seq = request.getParameter("seq");

		GuideItemHotDto gihDto = new GuideItemHotDto();
		gihDto.setItemHotId(SystemTools.getUUIDString());
		gihDto.setCatgHotId(Integer.parseInt(catgId));
		gihDto.setItemHotTitle(title);
		gihDto.setItemHotUrl(url);
		gihDto.setItemHotIconUrl("test");
		gihDto.setItemHotVisitCnt(0);
		gihDto.setItemHotSeq(Integer.parseInt(seq));

		ResultDto rd = iAdminGuideService.doAddHotItem(gihDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxLoadHotItem")
	@ResponseBody
	public Map<String, Object> adminAjaxLoadHotItem(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxLoadHotItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");

		GuideItemHotDto gihDto = new GuideItemHotDto();
		gihDto.setCatgHotId(Integer.parseInt(id));

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		PageDto pDto = new PageDto(Integer.parseInt(page), Integer.parseInt(rows));

		ResultDto rd = iAdminGuideService.getHotItemByCatgIdByPage(gihDto, pDto);
		if (rd.isSuccess()) {
			List<GuideItemHotDto> gihdList = rd.getDtoList("GuideItemHotDtoList");
			map.put("rows", gihdList);
			pDto = (PageDto) rd.getFirstDto("PageDto");
			map.put("total", pDto.getTotal());
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxModifyHotItem")
	@ResponseBody
	public Map<String, Object> adminAjaxModifyHotItem(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxModifyHotItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String url = request.getParameter("url");
		String seq = request.getParameter("seq");

		GuideItemHotDto gihDto = new GuideItemHotDto();
		gihDto.setItemHotId(id);
		gihDto.setItemHotTitle(title);
		gihDto.setItemHotUrl(url);
		gihDto.setItemHotSeq(Integer.parseInt(seq));

		ResultDto rd = iAdminGuideService.doModifyHotItem(gihDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("adminAjaxDeleteHotItem")
	@ResponseBody
	public Map<String, Object> adminAjaxDeleteHotItem(HttpServletRequest request) {
		logger.info("-------admin/guide/---adminAjaxDeleteHotItem-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");

		GuideItemHotDto gihDto = new GuideItemHotDto();
		gihDto.setItemHotId(id);

		ResultDto rd = iAdminGuideService.doDeleteHotItem(gihDto);
		if (rd.isSuccess()) {
			map.put("result", AdminGuideConstants.AJAX_SUCCESS);
		} else {
			map.put("result", AdminGuideConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
